package com.health.viewscontrols.medical.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nova.health.ble.callback.OnWriteCallback;
import com.nova.health.ble.device.MyBleDevice;
import com.nova.health.ble.receiver.MyBleBroadcastReceiver;
import com.novabluetooth.services.BluetoothConstant;
import com.novabluetooth.views.BluetoothDeviceBindActivity2;
import com.novabluetooth.views.ExamHistoryActivity;
import com.novabluetooth.views.ExamHistoryWeightActivity;

/**
 * 基本类
 * 
 * @author
 *
 */
public abstract class BaseDeviceActivity1 extends BasePtActivity implements IDeviceView {

	protected static final String TAG = "BaseDeviceActivity";

	protected LinearLayout ll_finish;
	protected TextView tv_title;
	protected LinearLayout ll_center_bg;
	protected TextView tv_operation_tips;
	protected TextView tv_state;
	protected TextView blueToothState;
	protected TextView tv_bind_device;
	protected TextView tv_check_history;
	protected TextView bt_data;
	protected TextView bt_delete_history;
	protected TextView tv_result;
	protected TextView bt_start;
	protected EditText et_height;
	protected ImageView show_img;
	protected DeviceEnum deviceEnum;
	//蓝牙是否连接
	protected boolean isConnected;

	@Override
	public int bindLayout() {
		return com.nova.health.ble.R.layout.activity_ble_check;
	}

	@Override
	public void initData(@Nullable Bundle bundle) {
		deviceEnum = (DeviceEnum) getIntent().getSerializableExtra("deviceBean");
	}

	@Override
	public void initView(Bundle savedInstanceState, View contentView) {

		ll_finish = contentView.findViewById(com.nova.health.ble.R.id.ll_finish);
		tv_title = contentView.findViewById(com.nova.health.ble.R.id.tv_title);
		ll_center_bg = contentView.findViewById(com.nova.health.ble.R.id.ll_center_bg);
		tv_operation_tips = contentView.findViewById(com.nova.health.ble.R.id.tv_operation_tips);
		tv_state =  contentView.findViewById(com.nova.health.ble.R.id.tv_state);
		blueToothState =  contentView.findViewById(com.nova.health.ble.R.id.tv_ble_status);
		tv_bind_device =  contentView.findViewById(com.nova.health.ble.R.id.tv_bind_device);
		tv_check_history =  contentView.findViewById(com.nova.health.ble.R.id.tv_check_history);
		bt_data =  contentView.findViewById(com.nova.health.ble.R.id.bt_data);
		bt_delete_history =  contentView.findViewById(com.nova.health.ble.R.id.bt_delete_history);
		tv_result =  contentView.findViewById(com.nova.health.ble.R.id.tv_result);
		bt_start =  contentView.findViewById(com.nova.health.ble.R.id.bt_start);
		et_height =  contentView.findViewById(com.nova.health.ble.R.id.et_height);
		show_img =  contentView.findViewById(com.nova.health.ble.R.id.show_img);

		tv_title.setText(deviceEnum.getDeviceName());
		ll_finish.setOnClickListener(this::onWidgetClick);
		tv_bind_device.setOnClickListener(this::onWidgetClick);
		bt_start.setOnClickListener(this::onWidgetClick);
		tv_check_history.setOnClickListener(this::onWidgetClick);
		ll_center_bg.setBackgroundResource(deviceEnum.getBg_url());
	}

	/**
	 * 视图点击事件
	 *
	 * @param view 视图
	 */
	public void onWidgetClick(final View view) {
		if (view.getId() == com.nova.health.ble.R.id.tv_bind_device) {
			binDevice();
		} else if (view.getId() == com.nova.health.ble.R.id.tv_check_history) {
			ckCLHistoryData(deviceEnum.getQueryIndex(), deviceEnum.getmTypename());
		} else if (view.getId() == com.nova.health.ble.R.id.bt_start) {
			startDevice();
		} else if (view.getId() == com.nova.health.ble.R.id.ll_finish) {
			finish();
		}
	}


	/**
	 * 查看历史记录
	 *
	 * @author 王婷婷
	 * @date 2017年7月14日 下午2:33:48
	 */
	public void ckCLHistoryData(String queryIndex,String mTypename) {
		String typename = BluetoothConstant.getCLTypeName(queryIndex);
		if (TextUtils.isEmpty(typename)) {
			showTextDialog("请先选择测量项目");
		} else {
			Intent intent2;
			if ("newTIZHONGQing".equals(queryIndex)) {
				intent2 = new Intent(this, ExamHistoryWeightActivity.class);
			} else {
				intent2 = new Intent(this, ExamHistoryActivity.class);
			}
			Log.e(TAG, "ckCLHistoryData:queryIndex   " +  queryIndex);
			Log.e(TAG, "ckCLHistoryData:queryIndex   " +  mTypename);
			if(BluetoothConstant.TRIPLE.equals(queryIndex)){
				//百捷设备需要 区分 三合一设备 还是 血红蛋白【两种展示测量历史记录 的 方式不一样】
				if(BluetoothConstant.HB_TYPENAME.equals(mTypename)){
					intent2.putExtra("flag", BluetoothConstant.HB_TYPENAME);
				}else{
					intent2.putExtra("flag", typename);
				}
			}else{
				intent2.putExtra("flag", typename);
			}
			startActivity(intent2);
		}
	}

	/**
	 * 绑定设备
	 */
	public void binDevice() {
		Intent intent = new Intent(this, BluetoothDeviceBindActivity2.class);
		startActivity(intent);
		finish();
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		disconnectDevice();
	}
}